package com.yanfan.usersmart.user.firemain.data;


import com.yanfan.usersmart.user.firemain.ByteUtil;
import com.yanfan.usersmart.user.firemain.NumConstant;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 设备状态
 * 部件状态
 *
 * @author TDS
 * @date 2022/08/23
 */
@Data
public class DeviceStatus   {


	private Integer status;

	private UserMessage userMessage;

	private String device_id;

	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DeviceStatus(String message,String deviceId){

		this.device_id = deviceId;

           userMessage = new UserMessage();
           userMessage.setSystemType(Integer.parseInt(message.substring(0,2),16));
            userMessage.setFireMainCode(Integer.parseInt(message.substring(2,4),16));
            userMessage.setPartType(Integer.parseInt(message.substring(4,6),16));
            userMessage.setAddressNo(String.valueOf(Integer.parseInt( message.substring(12,14)+message.substring(10,12)+
					message.substring(8,10)+
                    message.substring(6,8),16)));

            userMessage.setLoopNo(String.valueOf(Integer.parseInt(message.substring(20,22)+message.substring(18,20)
					+message.substring(16,18)+ message.substring(14,16),16)));
            String bit_stat = ByteUtil.buqi(Integer.toBinaryString(Integer.parseInt(message.substring(24,26)+message.substring(22,24),16)));
            status = 2;
            if (NumConstant.ONE.equals(bit_stat.substring(15,16))){
                userMessage.setPartStat("测试运行");
            }else if (NumConstant.ONE.equals(bit_stat.substring(14,15))){
                userMessage.setPartStat("火警");
				status = 1;
            }else if (NumConstant.ONE.equals(bit_stat.substring(13,14))){
                userMessage.setPartStat("故障");
                status = 0;
            }else if (NumConstant.ONE.equals(bit_stat.substring(12,13))){
                userMessage.setPartStat("屏蔽");
				status = 1;
            }else if (NumConstant.ONE.equals(bit_stat.substring(11,12))){
                userMessage.setPartStat("监管");
				status = 1;
            }else if (NumConstant.ONE.equals(bit_stat.substring(10,11))){
                userMessage.setPartStat("启动");

            }else if (NumConstant.ONE.equals(bit_stat.substring(9,10))){
                userMessage.setPartStat("反馈");
				status = 1;
            }else if (NumConstant.ONE.equals(bit_stat.substring(8,9))){
                userMessage.setPartStat("延时");
            }else if (NumConstant.ONE.equals(bit_stat.substring(7,8))){
                userMessage.setPartStat("电源故障");
				status = 0;
            }else if (NumConstant.ONE.equals(bit_stat.substring(6,7))){
                userMessage.setPartStat("手动");
            }else{
                userMessage.setPartStat("正常");
            }
            //设备台账信息没有，解析出来都是空格
            String addressDesc =ByteUtil.gB18030ByteArrayToUtf8String(ByteUtil.hexStringToByteArray(message.substring(26,106))).replace("\u0000","").replace("\uE5E5","");
            userMessage.setPartExplain(addressDesc);
			String partTypeExplain = ByteUtil.gB18030ByteArrayToUtf8String(ByteUtil.hexStringToByteArray(message.substring(106,146))).replace("\u0000","").replace("\uE5E5","");
			userMessage.setPartTypeExplain(partTypeExplain);

            String time=message.substring(146);
            String ms=String.valueOf(Integer.parseInt(time.substring(0,2),16));
            String min=String.valueOf(Integer.parseInt(time.substring(2,4),16));
            String hour=String.valueOf(Integer.parseInt(time.substring(4,6),16));
            String day=String.valueOf(Integer.parseInt(time.substring(6,8),16));
            String month=String.valueOf(Integer.parseInt(time.substring(8,10),16));
            String year=String.valueOf(Integer.parseInt(time.substring(10),16));

            userMessage.setUpTime("20"+year+"-"+month+"-"+day+" "+hour+":"+min+":"+ms);

    }

}
